insert value at the beginning of an array in php - php

my array is
$hello= array( Code => 'TIR', Description => 'Tires', Price => 100 )
now i want to add a value in array beginning of an array not the end of an array.... and results i want is
$hello= array( ref=>'World', Code => 'TIR', Description => 'Tires', Price => 100 )
UPDATE
actually i need any value that is coming will be added in the beginning of an array....this is not single value.. ref=world.... this is coming from output...like if i add quantity=50, then it should be added beginning of an array before 'ref' an array should be
$hello= array(quantity=>'50', ref=>'World', Code => 'TIR', Description => 'Tires', Price => 100 )

I would use array_merge()
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
$hello = array ("Code" => "Tir" .....); // you should really put quotes
// around the keys!
$world = array ("ref" => "World");
$merged = array_merge($world, $hello);

You can use the + operator:
$hello = array( 'Code' => 'TIR', 'Description' => 'Tires', 'Price' => 100 );
$hello = array('ref' => 'World') + $hello;
print_r($hello);
would give
Array
(
[ref] => World
[Code] => TIR
[Description] => Tires
[Price] => 100
)
Like Pekka said, you should put quotes around the keys. The PHP manual explicitly states omitting quotes is wrong usage. You might also want to check out my answer about the difference between using the + operator vs using array_merge to decide which you want to use.

$a= array( 'a' => 'a' );
$b = array( 'b' => 'b' );
$res = $b + $a;
//result: ( 'b' => 'b', 'a' => 'a' )

$hello = array_merge(array('ref'=>'World'), $hello);

Related

Fetching a multidimensional array

I am trying to edit a plugin that is fetching a multidimensional array, then breaking it out into a foreach statement and doing stuff with the resulting data.
What I am trying to do is edit the array before it gets to the foreach statement. I want to look and see if there is a key/value combination that exists, and if it does remove that entire subarray, then reform the array and pass it to a new variable.
The current variable
$arrayslides
returns several subarrays that look like something like this (I remove unimportant variables for the sake of briefness):
Array (
[0] => Array (
[slide_active] => 1
)
[1] => Array (
[slide_active] => 0
)
)
What I want to do is look and see if one of these subarrays contains the key slide_active with a value of 0. If it contains a value of zero, I want to dump the whole subarray altogether, then reform the multidimensional array back into the variable
$arrayslides
I have tried a few array functions but have not had any luck. Any suggestions?
$arrayslides = array(0 => array ( 'slide_active' => 1, 'other_data' => "Mark" ),
1 => array ( 'slide_active' => 0, 'other_data' => "ABCDE" ),
2 => array ( 'slide_active' => 1, 'other_data' => "Baker" ),
3 => array ( 'slide_active' => 0, 'other_data' => "FGHIJ" ),
);
$matches = array_filter($arrayslides, function($item) { return $item['slide_active'] == 1; } );
var_dump($matches);
PHP >= 5.3.0
I know its not so efficient but still
foreach ($arraySlides as $key => $value)
{
if(in_array('0', array_values($value))
unset($arraySlides[$key]);
}

php put arrays in arrays

I want to push some items in one array, here is the structure:
$str = 'String';
$a = array('some', 'sub', 'page');
and I want to push the items to some other array that should become:
Array (
[some] => Array (
[sub] => Array (
[page] => String
)
)
)
I don't know how exactly to explain it, so hope the example shows you something.
I want any new element in the first array (a) to be pushed as sub-array of the prevous one and the last to have the value from $str;
$string = 'My Value';
$my_first_array = array('my', 'sub', 'arrays');
Then some function to parse $my_first_array and transfer it as:
Example:
ob_start('nl2br');
$my_parsed_sub_array = parse_sub_arrays($my_first_array, $string);
print_r($my_parsed_sub_array);
===>>>
Array (
[my] => Array (
[sub] => Array (
[arrays] => String
)
)
)
[Edit] I hope that, this time, I've understood the question...
If you have your string and array like this :
$str = 'test';
$a = array('some', 'sub', 'page');
You could first initialize the resulting array this way, dealing with that special case of the last item :
$arr = array($a[count($a)-1] => $str);
Then, you can loop over each item of your $a array, starting from the end (and not working on the last item, which we've dealt with already) :
for ($i=count($a) - 2 ; $i>=0 ; $i--) {
$arr = array($a[$i] => $arr);
}
With this, dumping the resulting array :
var_dump($arr);
Should get you the expected result :
array
'some' =>
array
'sub' =>
array
'page' => string 'test' (length=4)
Old answer below, before understanding the question :
You could declare your array this way :
$arr = array(
'some' => array(
'sub' => array(
'page' => $str,
),
),
);
Or, using several distinct steps (might be easier, depending on the way you construct your sub-arrays, especially in a more complex case than the current example) :
$sub2 = array('page' => $str);
$sub1 = array('sub' => $sub2);
$arr = array('some' => $sub1);

PHP Multidimensional Array First Object

I have an array in PHP that looks like
Array ( [123654] => Array ( [0] => 123456789123456789 [1] => 1 [2] => 06/24/2011 [3] => 06/24/2012 [4] => 12355.44 [5] => 55321.55 ) )
I know in javascript I could access the data I need by doing array[0][0], how would I go about doing this in PHP. It is the 123456789123456789 value that I'm looking at getting.
Try this
array_slice($array, 0, 1);
http://php.net/array_slice
If you don't know the exact keys, you could do something like this:
$a = array_values($my_array);
$b = array_values($a[0]);
echo $b[0];
array_values replaces the keys by simple numbers from 0 to n-1 (where n is the count of values), by that you can access your desired value with the indexes [0][0]. See more here
http://codepad.org/YXu6884R
Here you go. See above for proof. The methodology from #azat is not explicit enough and is prone to risk if the elements of the array or sub array are re-arranged or if the key value for the super array changes.
$my_array = array( 123654 => array( 0 => '123456789123456789', 1 => '1', 2 => '06/24/2011', 3 => '06/24/2012', 4 => '12355.44', 5 => '55321.55' ) );
echo $my_array['123654'][0];
Try
$first = array_shift(array_values($array));
http://php.net/manual/en/function.array-shift.php

Add up content of array to another array in php

I want to have an array that contains a list of something, then I have another array that contains a list of something. I want to add those arrays up to each other.
For example, I have this
<?php
$greetings1 = array (
'a' => 'hello',
'b' => 'hi'
);
$greetings2 = array ('c' => 'hey',
'd' => 'greetings'
);
array_push($greetings1, $greetings2);
foreach($greetings1 as $a => $b) {
echo $a.' and '.$b."<br/>";
}
?>
I want it so that the output is:
a and hello
b and hi
c and hey
d and greetings
the real output of the php code above is:
a and hello
b and hi
0 and Array
So how do I properly add the two arrays up?
Thanks!
You can array_merge
<?php
$greetings1 = array(
'a' => 'hello',
'b' => 'hi',
);
$greetings2 = array(
'c' => 'hey',
'd' => 'greetings',
);
$greetings = array_merge($greetings1, $greetings2);
Which will output:
Array
(
[a] => hello
[b] => hi
[c] => hey
[d] => greetings
)
array_merge($greetings1, $greetings2);
array_push just adds an element at the end of the array (in that case another array).
You're looking for array_merge

PHP - How can I assign a name to my array key instead of an int with array_push

Hey everyone, I have a database result that returns from a method. I need to push 4 more values onto the stack but I need to name the keys. array_push() automatically assigns an int. How can I overcome this behavior?
Array
(
[these] => df
[are] => df
[the] => sdf
[keys] => sd
[ineed] => daf
[0] => something
[1] => something
[2] => something
[3] => something
)
The keys that are int values need to be changed. How can I do this using array_push?
Just like this:
$arr['anotherKey'] = "something";
$arr['yetAnotherKey'] = "something";
$arr['andSoOn'] = "something";
or
$arr = array_merge($arr, array(
'anotherKey' => "something",
'yetAnotherKey' => "something",
'andSoOn' => "something"
));
...but I'd recommend the first method, since it merely adds more elements to the array, whereas the second will have a lot more overhead (though it's much more flexible in some situations).
If the four values you want to use are already in an associative array themselves, you can use + to merge the two arrays:
$array1 = array('these' => ..., 'are' => .., 'keys' => ...);
$four_entries = array('four' => ..., 'more' => ..., 'keys' => ..., '!' => ...);
$merged_array = $array1 + $four_entries;
Why not
$arr["whateveryouwant"] = something
Note: If you use array_push() to add
one element to the array it's better
to use $array[] = because in that way
there is no overhead of calling a
function.
If you want to add more entries to the array, all you need to do is:
Existing array;
$array =
{
"these" => "df"
"are" => "df"
"the" => "sdf"
"keys" => "sd"
"ineed" => "daf"
}
Adding to the array
$array["new_key1"] = "something";
$array["new_key2"] = "something";
If you want to assign the name you do not use the array_push function, you just assign the element:
$array['somekey'] = 'somevalue';
So, in short, you can't do that using array_push.

Categories