Member Avatar for LastMitch

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!

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?

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

Member Avatar for diafol

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.

Member Avatar for LastMitch

@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.

Member Avatar for LastMitch

@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.

Member Avatar for LastMitch

@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.

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');
Member Avatar for LastMitch

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.

Member Avatar for LastMitch

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);
}

}

?>

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'))
commented: Thanks for the example! +5
Member Avatar for LastMitch

@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?

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

Member Avatar for LastMitch

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

maybe. Try cause this:

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

29 line does the semicolon at the end of line

Member Avatar for LastMitch

@radow

I will make some adjusted base on changes:

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

-

29 line does the semicolon at the end of line
Member Avatar for LastMitch

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?

will add in end your script

var_dump($cheesecake);

you should will see sorted array

Member Avatar for LastMitch

OK I will used var_dump($cheesecake);

Member Avatar for LastMitch

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

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

Member Avatar for LastMitch

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?

Member Avatar for LastMitch

@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
                (
                )
        )
    )
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.