homepage Welcome to WebmasterWorld Guest from 54.204.59.230
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Problem using array and array slice
Sub_Seven




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

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




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

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




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

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




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

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 :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved