Welcome to WebmasterWorld Guest from 54.226.194.180

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Problem using array and array slice

     

Sub_Seven

4:38 am on May 15, 2014 (gmt 0)

5+ Year Member



Hello people,

Please look at the following code:

Note: There are three items in DB where type = 0.

$aVPs = DB::getInstance()->query("SELECT * FROM vp WHERE type = 0");

foreach($aVPs->results() as $aPackage) {
$name = ["name" => escape($aPackage->name)];
}

var_dump(array_slice($name, 0));


This returns:

array(1) {
["name"]=>
string(5) "data1"
}


Shouldn't this return all 3 items since I'm not specifying length?

Now, if I change the following line:
$name = ["name" => escape($aPackage->name)];


For:
$name[$aPackage->vp_id] = ["name" => escape($aPackage->name)];


I get the following:

array(3) {
[0]=>
array(1) {
["name"]=>
string(5) "data1"
}
[1]=>
array(1) {
["name"]=>
string(5) "data2"
}
[2]=>
array(1) {
["name"]=>
string(5) "data3"
}
}


But if I try:
foreach(array_slice($name, 0) as $aPackageName) {
echo $aPackageName . '<br>';
}


I get:
Array<br>Array<br>Array<br>


I believe that since all 3 inner arrays have the same index that could be one of the problems, the other problem may be that on the foreach statement I'm only accessing the data of array(3) (the one encapsulating the arrays array(1)) so maybe modifying the foreach statement to access the inner arrays would do it.

I don't know, I'm just guessing after trying different things for a while.

Could someone tell me why it wont work, there has to be a way.

Thanks in advance for any help provided.

penders

10:14 am on May 17, 2014 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



Shouldn't this return all 3 items since I'm not specifying length?


No, because inside your preceding foreach loop you are overwriting (not appending to) your $name variable. At the end of your loop, it only contains the last assigned result. var_dump($name) should return the same result.

But if I try: ....
I get Array<br>Array<br>Array<br>


Yes, because $aPackageName is an array and you are attempting to echo this. When PHP converts an array to a string you get the string "Array" (and possibly an E_NOTICE in PHP 5.4+ in this instance). The array you are stepping through is a two-dimensional array (an array of arrays).

In your foreach, try:
echo $aPackageName['name'].'<br>';

(To access the 'name' element of each inner array.)

I believe that since all 3 inner arrays have the same index that could be one of the problems


This should not be a problem. In fact, this perfectly correct.

the other problem may be that on the foreach statement I'm only accessing the data of array(3) (the one encapsulating the arrays array(1)) so maybe modifying the foreach statement to access the inner arrays would do it.


Yes, as above.

I'm not sure why you are using array_slice() in your code above (unless there is a bigger picture)? array_slice($name,0) simply returns the entire array, the same as if you'd simply used
$name
directly.

penders

2:32 pm on May 17, 2014 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



... inside your preceding foreach loop you are overwriting (not appending to) your $name variable.


Just to add, to append another element to the array you can use empty square bracket syntax [], for example:
$name[] = ...

(This adds the next numeric index: 0, 1, 2, etc.)

Or, do what you did in your second example and explicitly define the elements index:
$name[$aPackage->vp_id] = ...

Sub_Seven

7:05 pm on May 19, 2014 (gmt 0)

5+ Year Member



Thanks a lot penders

I think I ended up trying array_slice probably because I had too much time trying; I ended up with a much simpler solution:

<?php
foreach($aVPs->results() as $aPackage) {
echo escape($aPackage->name);
}
?>

Thanks so much for the knowledge and help :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month