0

Hi,

I'm having issue with understanding how this array works in OOP. I want to sort this from ABC or by 123. I'm reading about it but I don't know how to run this code. I created the array but making it work is something new to me.

//This is my Array
$cheesecake = array(1 => 'Apple CheeseCake', 2 => 'Blueberry CheeseCake', 3 => 'Strawberry CheeseCake', 4 => 'Mango CheeseCake', 5 => 'Raspberry CheeseCake');

//This is my Function that sort the array
function sortObjectByKey($object,$cheesecake =''){

 $temp = array();
 foreach($object as $key => $valve){
     $temp[$key] = '';
 }

 if($cheesecake == 'ASC'){
     sortC($temp);
 } else {
     sortCa($temp);
 }

 $tempObject = new stdClass();
 foreach($temp as $key => $valve){
     $tempObject ->$key = $object-> $key;
 }

 return $tempObject;

 }

Any Suggestions and explanation will help. I appreciate it. Thanks!

4
Contributors
23
Replies
24
Views
4 Years
Discussion Span
Last Post by LastMitch
Featured Replies
  • 1
    jkon 491   4 Years Ago

    I can’t see any object there …. except from the stdClass that is used for typecasting to object. What exactly are you trying to do? Read More

  • You can to use functions uasort(), uksort or usort(). Read More

  • 1
    diafol 3,669   4 Years Ago

    Yes, like jkon, I'm a bit confused too. You're passing an array to the function, although you've called it 'object'. sortC and sortCa don't exist in php, unless you have them as user-defined functions. http://php.net/manual/en/function.sort.php I don't know what you're trying to do either. The first loop sets all keys … Read More

  • ok. method strcmp() take two parametrs [strcmp()](http://php.net/manual/ru/function.strcmp.php). Your method sortByName() should look like a following way public static function sortByName(CheeseCake $c1, CheeseCake $c2) { return strcmp($c1->name, $c2->name); } $cheesecake = array(new CheeseCake("Apple Cheesecake"), new CheeseCake("Strawberry Cheesecake"), new CheeseCake("Mango Cheesecake")); usort($cheesecake, array('CheeseCake','sortByName')) Read More

1

I can’t see any object there …. except from the stdClass that is used for typecasting to object. What exactly are you trying to do?

1

You can to use functions uasort(), uksort or usort().

Edited by radow

1

Yes, like jkon, I'm a bit confused too. You're passing an array to the function, although you've called it 'object'. sortC and sortCa don't exist in php, unless you have them as user-defined functions.

http://php.net/manual/en/function.sort.php

I don't know what you're trying to do either. The first loop sets all keys to ''. Odd. The you pass this info to the 'class' loop.

If you need to get rid of keys for some reason, you can use array_values(), but this will still hold the same 'keys' just starting at 0 instead of 1. Have a look at radow's suggestions.

0

@jkon

I can’t see any object there …. except from the stdClass that is used for typecasting to object. What exactly are you trying to do?

Thanks for the reply & explanation. I'm trying to sort the array.

0

@radow

You can to use functions uasort(), uksort or usort().

Thanks for the reply & example. I'm not going to used that because on the Manaul it said

Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results.

0

@diafol

Thanks for the reply & explanation!

I'm not going to used that because on the Manaul it said

Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results.
0

You can will implement your method sorting and use its together with method usort.

function sortDates($a, $b)
{
    $timestamp_a = is_int($a) ? $a : strtotime($a);
    $timestamp_b = is_int($b) ? $a : strtotime($b);
    if($timestamp_a == $timestamp_b)
    {
        return 0;
    }

    return $timestamp_a < $timestamp_b ? -1 : 1;
}

$date = array(1001203200, "10 September 2010", "09/23/2001 00:00:00");

usort($date, 'sortDates');
0

OK, Since I got confused with the function so I modify the code into 2 different type of sorting array .

Here is the first example I ended up using sort() function anyway:

<?php
//This is my Array
$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");
//This is to Sort 
sort($cheesecake);
//Foreach $key is assigned
foreach ($cheesecake as $key => $val) {

   echo $cheesecake[" .$key "]= "$val<br/>";

}

?>

When I ran the code my cheesecakes were in order:

Apple Cheesecake
Blueberry Cheesecake
Mango Cheesecake
Raspberry Cheesecake
Strawberry Cheesecake

So this works! Thanks to everyone explanation.

Edited by LastMitch: grammer

0

Here is my second example:

When I ran this code, my page went blank. Any explanation why?

<?php
//This is my Array
$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

class CheeseCake
{
private $name;

public function CheeseCake($name)
{
$this->name = $name;
}

public static function sortByName(CheeseCake $c1)
{
return strcmp($c1->name);
}

private $cheesecake = array();

public function addCheeseCake ($name)
{
$this->cheesecake[] = new CheeseCake($name);
}

public function display()
{
print_r($this->cheesecake);
}

}

?>

Edited by LastMitch: grammer

1

ok. method strcmp() take two parametrs strcmp().
Your method sortByName() should look like a following way

public static function sortByName(CheeseCake $c1, CheeseCake $c2)
    {
        return strcmp($c1->name, $c2->name);
    }



$cheesecake = array(new CheeseCake("Apple Cheesecake"), new CheeseCake("Strawberry Cheesecake"), new CheeseCake("Mango Cheesecake"));

usort($cheesecake, array('CheeseCake','sortByName'))

Edited by radow

Votes + Comments
Thanks for the example!
0

@radow

I make some adjusted base on changes:

<?php

$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

class CheeseCake
{
private $name;
public function CheeseCake($name)
{
$this->name = $name;
}
public static function sortByName(CheeseCake $c1, CheeseCake $c2)
{
 return strcmp($c1->name, $c2->name);
}

private $cheesecake = array();
public function addCheeseCake ($name)
{
$this->cheesecake[] = new CheeseCake($name);
}

public function display()
{
print_r($this->cheesecake);
}
}

$cheesecake = array(new CheeseCake("Apple Cheesecake"), new CheeseCake("Blueberry Cheesecake"), new CheeseCake("Strawberry Cheesecake"), new CheeseCake("Raspberry Cheesecake"), new CheeseCake("Mango Cheesecake"))

usort($cheesecake, &array("CheeseCake","sortByName"));

?>

It didn't work either. There's an error in usort

Any suggestion and explanation?

0

in function usort() second parameter will not should passed by reference.
what did display error?
how look like array $cheesecake after applying function usort.

0

I got forgot to post the error:

unexpected 'usort'

It's means it something wrong here:

&array("CheeseCake","sortByName")

which I don't know how to fixed

0

@radow

I will make some adjusted base on changes:

usort($cheesecake, array('CheeseCake','sortByName'))

-

29 line does the semicolon at the end of line

Edited by LastMitch: grammer

0

I make some adjusted base on changes and when I ran it appear a blank page again

<?php

$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

class CheeseCake
{
private $name;
public function CheeseCake($name)
{
$this->name = $name;
}
public static function sortByName(CheeseCake $c1, CheeseCake $c2)
{
 return strcmp($c1->name, $c2->name);
}

private $cheesecake = array();
public function addCheeseCake ($name)
{
$this->cheesecake[] = new CheeseCake($name);
}

public function display()
{
print_r($this->cheesecake);
}
}

$cheesecake = array(new CheeseCake("Apple Cheesecake"), new CheeseCake("Blueberry Cheesecake"), new CheeseCake("Strawberry Cheesecake"), new CheeseCake("Raspberry Cheesecake"), new CheeseCake("Mango Cheesecake"));

usort($cheesecake, array('CheeseCake','sortByName'))

?>

Any suggestion and explanation?

Edited by LastMitch: grammer

0

will add in end your script

var_dump($cheesecake);

you should will see sorted array

Edited by radow

0

OK I will used var_dump($cheesecake);

0

I'm a bit surprise that var_dump() was used.

This is what it dump:

array(5) { [0]=> object(CheeseCake)#1 (2) { ["name":"CheeseCake":private]=> string(16) "Apple Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } [1]=> object(CheeseCake)#2 (2) { ["name":"CheeseCake":private]=> string(20) "Blueberry Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } [2]=> object(CheeseCake)#5 (2) { ["name":"CheeseCake":private]=> string(16) "Mango Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } [3]=> object(CheeseCake)#4 (2) { ["name":"CheeseCake":private]=> string(20) "Raspberry Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } [4]=> object(CheeseCake)#3 (2) { ["name":"CheeseCake":private]=> string(21) "Strawberry Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } }

This output looks weird

Edited by LastMitch: grammer

0

What is strange?
Objects in the array sorted by ABC.
What and it's necessary was to do.

0

What is strange?
Objects in the array sorted by ABC.
What and it's necessary was to do.

When the output it should be more organized than it should be. That was the issue.

The output should look like this:

array(5) { 
[0]=> object(CheeseCake)#1 (2) { 
["name":"CheeseCake":private]=> string(16) "Apple Cheesecake"   ["cheesecake":"CheeseCake":private]=> array(0) { } } 
[1]=> object(CheeseCake)#2 (2) { 
["name":"CheeseCake":private]=> string(20) "Blueberry Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } 
[2]=> object(CheeseCake)#5 (2) { 
["name":"CheeseCake":private]=> string(16) "Mango Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } 
[3]=> object(CheeseCake)#4 (2) { 
["name":"CheeseCake":private]=> string(20) "Raspberry Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } 
[4]=> object(CheeseCake)#3 (2) { 
["name":"CheeseCake":private]=> string(21) "Strawberry Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } 
}

Not this:

array(5) { [0]=> object(CheeseCake)#1 (2) { ["name":"CheeseCake":private]=> string(16) "Apple Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } [1]=> object(CheeseCake)#2 (2) { ["name":"CheeseCake":private]=> string(20) "Blueberry Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } [2]=> object(CheeseCake)#5 (2) { ["name":"CheeseCake":private]=> string(16) "Mango Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } [3]=> object(CheeseCake)#4 (2) { ["name":"CheeseCake":private]=> string(20) "Raspberry Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } [4]=> object(CheeseCake)#3 (2) { ["name":"CheeseCake":private]=> string(21) "Strawberry Cheesecake" ["cheesecake":"CheeseCake":private]=> array(0) { } } }

How do I make it organized when I used var_dump(). Usually when you dump a file it's usually not organized. Is there another way to echo it out correctly?

Edited by LastMitch: grammer

0

@radow

Nevermind I figure it out! I used print_r() function instead of var dump() function plus I put <pre> tags

Thanks for being patience and helping me out with this issue. Thanks!

Here is my example:

<pre>
<?php
$cheesecake = array("Apple Cheesecake", "Blueberry Cheesecake", "Strawberry Cheesecake", "Mango Cheesecake", "Raspberry Cheesecake");

class CheeseCake
{
private $name;
public function CheeseCake($name)
{
$this->name = $name;
}
public static function sortByName(CheeseCake $c1, CheeseCake $c2)
{
 return strcmp($c1->name, $c2->name);
}

private $cheesecake = array();
public function addCheeseCake ($name)
{
$this->cheesecake[] = new CheeseCake($name);
}

public function display()
{
print_r($this->cheesecake);
}
}

$cheesecake = array(new CheeseCake("Apple Cheesecake"), new CheeseCake("Blueberry Cheesecake"), new CheeseCake("Strawberry Cheesecake"), new CheeseCake("Raspberry Cheesecake"), new CheeseCake("Mango Cheesecake"));

usort($cheesecake, array('CheeseCake','sortByName'));

print_r($cheesecake)
?>
</pre>

Here is my output which I want it to appear as:

Array
(
    [0] => CheeseCake Object
        (
            [name:CheeseCake:private] => Apple Cheesecake
            [cheesecake:CheeseCake:private] => Array
                (
                )

        )

    [1] => CheeseCake Object
        (
            [name:CheeseCake:private] => Blueberry Cheesecake
            [cheesecake:CheeseCake:private] => Array
                (
                )

        )

    [2] => CheeseCake Object
        (
            [name:CheeseCake:private] => Mango Cheesecake
            [cheesecake:CheeseCake:private] => Array
                (
                )

        )

    [3] => CheeseCake Object
        (
            [name:CheeseCake:private] => Raspberry Cheesecake
            [cheesecake:CheeseCake:private] => Array
                (
                )

        )

    [4] => CheeseCake Object
        (
            [name:CheeseCake:private] => Strawberry Cheesecake
            [cheesecake:CheeseCake:private] => Array
                (
                )
        )
    )
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.