Hello
I have an array with several things in it.
How can I get each of the items in that array into a series of 5 variables of the following names and in this order:
$addrp1
$addrp2
$addrp3
$addrp4
$addrp5
So if the array only contains 3 things, the first 3 will be entered in $addrp1 through to $addrp3 and $addrp4 and $addrp5 will be blank variables.
Any thoughts?
Neil
smftre
March 8, 2011, 5:19pm
2
Wow, now that’s a “construction of a question!”
<?php
$array = array(“one”, “two”, “three”);
$arrItem1 = $array[0];
$arrItem2 = $array[1];
$arrItem3 = $array[2];
echo $arrItem1;
Something like that?
Is there a reason you cannot access them directly form within the array? It would be cleaner IMO.
<?php
error_reporting(-1);
ini_set('display_errors', true);
$array = array(
'one',
'two',
'three'
);
for($index = 0; $index < 5; $index++){
${'variable' . $index} = isset($array[$index]) ? $array[$index] : '' ;
}
echo '$variable0 = ', $variable0, PHP_EOL ;
echo '$variable1 = ', $variable1, PHP_EOL ;
echo '$variable2 = ', $variable2, PHP_EOL ;
echo '$variable3 = ', $variable3, PHP_EOL ;
echo '$variable4 = ', $variable4, PHP_EOL ;
/*
$variable0 = one
$variable1 = two
$variable2 = three
$variable3 =
$variable4 =
*/
smftre:
Wow, now that’s a “construction of a question!”
<?php
$array = array(“one”, “two”, “three”);
$arrItem1 = $array[0];
$arrItem2 = $array[1];
$arrItem3 = $array[2];
echo $arrItem1;
Something like that?
That’s perfect. All working now. Thanks
What you’re looking for is list()
$arr = array( 'a', 'b', 'c' );
list( $q, $w, $e, $r, $t ) = $arr;
$q will have ‘a’, $w will have ‘b’, $e will have ‘c’, $r and $t will be NULL
QMonkey:
What you’re looking for is list()
$arr = array( 'a', 'b', 'c' );
list( $q, $w, $e, $r, $t ) = $arr;
$q will have ‘a’, $w will have ‘b’, $e will have ‘c’, $r and $t will be NULL
Whilst that would work, and it’s a few less lines of code than my own offering, PHP will throw two ‘Undefined Offset’ notices.
QMonkey:
What you’re looking for is list()
$arr = array( 'a', 'b', 'c' );
list( $q, $w, $e, $r, $t ) = $arr;
Wow that’s awesome and very simple! Thanks
$q will have ‘a’, $w will have ‘b’, $e will have ‘c’, $r and $t will be NULL
Oh right, how can the “Undefined Offset” notices be resolved? Is there a way to set the variables not being used to be null or something?
Thanks
Neil
smftre
March 9, 2011, 12:42pm
8
list ( $q , $w , $e ) = $arr ;
But if I use that then what happens if I have 5 items in my array?
smftre
March 9, 2011, 1:46pm
10
Then you would use 5 items
If you are having problems due to it being a dynamic array length, then you may want to rather not use list() and just do a for loop on the array and step through it one by one.
I’m not actually sure what you are having a problem with throughout this thread. Am I missing something?
Right, it would throw notices - which indicates that’s not the right thing to do. Thanks for noting that.
Depending on your situation, something like this will work:
$arr = array( 'a', 'b', 'c' );
$arr = $arr + array_fill( 0, 5, null );
list( $q, $w, $e, $r, $t ) = $arr;
That array_fill() is assuming your original array has consecutive numeric indexes starting at zero. If that’s not the case, look at array_fill_keys() OR convert your original array into one like that with array_values()
$arr = array_values( $arr ) + array_fill( 0, 5, null );