Page 1 of 1

Array splice with multidimensionals??

Posted: Mon Mar 28, 2005 5:52 am
by Perfidus
I want to add some variables in an specific part of an array which inside another:

Code: Select all

$distances=array(array(1,0),array(33,44,89),array(568,88,99));
$top=count($distances);
echo $top;
for($m=$key;$m<$top;$m++){
array_splice($distances[$m], $key, 0, "$changearray[$m]");
}
I allways get the following message:

Warning: array_splice(): The first argument should be an array

Any hints???

Posted: Mon Mar 28, 2005 7:58 am
by Chris Corbyn

Code: Select all

$distances=array(array(1,0),array(33,44,89),array(568,88,99));
$top=count($distances);
echo $top;
for($m=0;$m<$top;$m++){
	array_splice($distances[$m], $key, 0, "$changearray[$m]");
}
It works if $key is defined....

Posted: Mon Mar 28, 2005 8:22 am
by Perfidus
My code was too small, sorry, check this out:

First I recover from DB an array that was serialized before being uploaded to database. Key comes from a form in the page before also does "$newarray" and "$changearray"

Code: Select all

$result= mysql_query("SELECT distances FROM arrays_measures" ,$link);
while ($row = mysql_fetch_array($result)){
$distances=unserialize($row['distances']);
}
array_splice($distances, $key, 0, "$newarray");//This allows me to do it!
$top=count($distances);
for($m=$key;$m<$top;$m++){
array_splice($distances[$m], $clave, 0, "$cambiarraybeta[$m]");//This NOT
}
The array in database is like the one I have already described before, but bigger:
$distances=array(array(1,0),array(33,44,89),array(568,88,99));

Posted: Mon Mar 28, 2005 8:40 am
by Perfidus
The worst thing of all is that if I try to echo distancias[$m], it returns Arrays:

Code: Select all

for($m=$key;$m<$top;$m++){
echo distancias[$m]."<br>";
array_splice($distances[$m], $clave, 0, "$cambiarraybeta[$m]");
This echoes:
Array
Array
Array
Array
ETC...
So why it gives me the message
Warning: array_splice(): The first argument should be an array ????

Posted: Mon Mar 28, 2005 8:46 am
by Chris Corbyn
Yeah I'm confused too.

Have you tried adding something like: :?

If it will print an array I can't see why array_splice won't agree that it's an array.

Code: Select all

$dummy_array = array();
$dist_array = array_merge($distances[$m], $dummy_array);
This should be funny - make it choke itself:

Code: Select all

if (is_array($distances[$m])) {
    //... do the stuff
}

Posted: Mon Mar 28, 2005 9:09 am
by Perfidus
Ok, fasten your seatbelts!

Code: Select all

for($m=$key;$m<$top;$m++){
if (is_array($distances[$m])) {echo "Yes, I,m a <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> array"; }
echo $distances[$m]."<br>";
$value=$distances[$m];
array_splice($value, $key, 0, "$hangearray[$m]");
}
It echoes:
Array
Array
Array
Array
Yes, I,m a <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> arrayArray
Yes, I,m a <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> arrayArray
Yes, I,m a <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> arrayArray
Yes, I,m a <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> arrayArray
Yes, I,m a <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> arrayArray
Yes, I,m a <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> arrayArray
Yes, I,m a <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> arrayArray
Yes, I,m a <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> arrayArray

Posted: Mon Mar 28, 2005 9:38 am
by Chris Corbyn
Hmm... starting to think it may be easier just to rebuild the array within the loop and use your newly built one :?

Posted: Mon Mar 28, 2005 9:48 am
by Perfidus
But??
What happened to the array?
I can get the info from different aplications, it works fine.
What's wrong in this case?

Posted: Mon Mar 28, 2005 10:57 am
by Perfidus
I've got some hints already:

Code: Select all

$distances=array(array(1,2), array(2,3), array(3,4));
$moredistances=array(5,6);
array_splice($distances, 3, 0, $moredistances);
?>
Doing this, what I would like to get is:

$distances=array(array(1,2), array(2,3), array(3,4), array(5,6));

But what I really seem to get is:

$distances=array(array(1,2), array(2,3), 5, 6));

Posted: Mon Mar 28, 2005 1:53 pm
by feyd

Code: Select all

$distances=array(array(1,2), array(2,3), array(3,4));
$moredistances=array(5,6);
$newarray = $distances + $moredistances;

var_export($newarray);