How to remove first dimension in multidimension array
array(
[0] => array(
[0]->1
[1]->2
)
)
Now echo
echo id[0][1];
how to get
echo id[0];
Use array_shift
$new_array = array_shift($array);
This is a bit trivial. Putting answer in different format than original answer above to not hide logic of what needs to happen behind a function call (though other answer is totally valid).
$new_array = $old_array[0];
var_dump($new_array);
Related
Suppose I've got the following string:
) [6] => Array ( [2014-05-05 00:0] => My actual content
If I want to only be left with My actual content at the end, what is the best way to split the entire string?
Note: the words My actual content are and can change. I'm hoping to cut the string based on the second => string as this will be present at all times.
It seems you're just looking to find the first value of an array with keys you do not know. This is super simple:
Consider the following array:
$array = array(
'2014-05-22 13:36:00' => 'foo',
'raboof' => 'eh',
'qwerty' => 'value',
'8838277277272' => 'test'
);
Method #1:
Will reset the array pointer to the first element and return it.
Using reset:
var_dump( reset($array) ); //string(3) "foo"
DEMO
Method #2:
Will reset the entire array to use keys of 0, 1, 2, 3...etc. Useful if you need to get more than one value.
Using array_values:
$array = array_values($array);
var_dump( $array[0] ); //string(3) "foo"
DEMO
Method #2.5:
Will reset the entire array to use keys of 0, 1, 2, 3...etc and select the first one into the $content variable. Useful if you need to get more than one value into variables straight away.
Using list and array_values:
list( $content ) = array_values($array);
var_dump( $content ); //string(3) "foo"
DEMO
Method #3:
Arrays are iteratable, so you could iterate through it but break out immediately after the first value.
Using a foreach loop but break immediatly:
foreach ($array as $value) {
$content = $value;
break;
}
var_dump($content); //string(3) "foo"
DEMO
To Answer your question, on extracting from a string based on last 'needle'...
Okay, this is quite an arbitrary question, since it seems like you're showing us the results from a print_r(), and you could reference the array key to get the result.
However, you mentioned "... at the end", so I'm assuming My actual content is actually right at the end of your "String".
In which case there's a very simple solution. You could use: strrchr from the PHP manual - http://www.php.net/manual/en/function.strrchr.php.
So you're looking at this: strrchr($string, '=>');
Hope this answers your question. Advise otherwise if not please.
you have to use foreach loop in a foreach to get the multi dimentional array values.
foreach($value as $key){
foreach($key as $val){
echo $val;
}
}
My array looks like this:
Array
( [myarr] => Array (
[504] => 2
[508] => 25
)
)
Is it possible to echo a certain position of this array? I have tried:
echo $_SESSION['myarr'][0][0];
I can't seem to get anything to echo back.
EDIT: to be more specific.. Is it possible to echo it based on numeric index?
Use array_keys() to get the keys into an array. Then access the 2D array using indexes in the keys array. Not that this is the best way to do this but it is a way to use numeric indexes to solve your problem.
$keys = array_keys($_SESSION["myarr"]);
$zero = $_SESSION["myarr"][$keys[0]];
It's just a regular nested array. You use the index keys just as you normally would:
echo $_SESSION['myarr'][504]; //2
echo $_SESSION['myarr'][508]; //25
Have a look at Get the first element of an array.
The following should work (untested, so no guaranties):
echo array_shift(array_values($_SESSION))[0][0];
Yes it is possible
print $array['myarr'][508]; // 25
This is getting ugly.
$i = 1;
foreach ($myarr as $array) {
if ($i == 2) echo $array;
$i++;
}
I have an array, and when i echo it i get the following output;
Array[{"name":"Kat","age":"10"}]
Now, i need to add an additional filed into this; so finally it should appear as;
Array[{"message":"Success","name":"Kat","age":"10"}]
if $arr is my array, how am i going to append "message":"Success" ?
Sorry, i don't have any code to demonstrate my workings, i am stuck here. i would appreciate it if anyone can help me.
Your array content looks like JSON to me. But, if your array are actually just PHP arrays, then do:
$arr = array('name' => 'Kat', 'age' => '10');
$arr['message'] = 'Success';
If it is a JSON encoded array:
$arr = json_decode('{"name":"Kat","age":"10"}' , true)); //true decodes to an array and not a standard object
$arr['message'] = 'Success';
echo $arr;
//If you want it back in JSON
$json = json_encode($arr);
echo $json;
Like Waygood said if you want to add a value to the end of an array just use:
$array[] = $value; or $array['somekey'] = $somevalue;
However, if you need to add a value to the beginning of the array (like your example) you can use:
array_unshift($array, $value);
Alternatively, if you need to add a key and a value to the beginning you can simply make an array with the key => value pair and merge the two arrays like so:
$firstArray = array("message" => "Success");
$newArray = array_merge($firstArray, $secondArray);
For reference, here are the links to the php.net documentation:
array_unshift
array_merge
what about $arr["message"]="Success";?
To add a named field, you can just use this:
$array['message'] = 'Success';
To add a unnamed field, this is the way to do it:
$array[] = $value;
There are multiple ways of doing it.
PHP array functions
http://php.net/manual/en/function.array-push.php
http://php.net/manual/en/function.array-merge.php
The + operator
Or like others mentioned using $arg['var'] =
Just define "message":"Success" as another array and use push, merge or +
Also looks like you have it Json encoded. You will need to handle that as well.
The array functions only work with regular arrays, not encoded strings.
Try this:
$arr = array("name"=>"Kat","age"=>"10");
print_r($arr);
$arr = array_merge(array("message" => "Success"), $arr);
print_r($arr);
All I need to two is remove the final two elements from an array, which only contains 3 elements, output those two removed elements and then output the non-removed element. I'm having trouble removing the two elements, as well as keeping their keys.
My code is here http://pastebin.com/baV4fMxs
It is currently outputting : Java
Perl
Array
I want it to output:
[One] => Perl and [Two] => Java
[Zero] => PHP
$last=array_splice($inputarray,-1);
//$last has now key=>value of last element
$middle=array_splice($inputarray,-1);
//$middle has now key=>value of middle element
//$inputarray has now only key=>value of first element
It seems to me that you want to retrieve those two values being removed before getting rid of them. With this in mind, I suggest the use of array_pop() which simultaneously returns the last item in the array and removes it from the array.
$val = array_pop($my_array);
echo $val; // Last value
$val = array_pop($my_array);
echo $val; // Middle value
// Now, $my_array has only the first value
You mentioned keys, so I assume it's an associative array. In order to remove an element, you can call the unset function, like this:
unset ($my_array['my_key'])
Do this for both elements that you want to remove.
array_pop will do it for you quite easily:
$array = array( 'one', 'two', 'three');
$last = array_pop( $array); echo $last . "\n";
$second = array_pop( $array); echo $second . "\n";
echo $array[0];
Output:
three
two
one
Demo
I don't want to use array_merge() as it results in i misunderstood that all values with the same keys would be overwritten. i have two arrays
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
and would like to combine them resulting like this
array(0=>'foo', 1=>'bar',2=>'bar', 3=>'foo');
array_merge() appends the values of the second array to the first. It does not overwrite keys.
Your example, results in:
Array (
[0] => foo
[1] => bar
[2] => bar
[3] => foo )
However, If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.
Unless this was just an example to another problem you were having?
Does this answer your question? I'm not sure exactly what you're trying to accomplish, but from your description it sounds like this will work:
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
foreach ($array2 as $i) {
$array1[] = $i;
}
echo var_dump($array1);
If someone stumbles upon this, this is a way to do it nowadays:
var_dump(array_merge_recursive($array1, $array2));
There are probably much better ways but what about:
$newarray= array();
$array1 = array(0=>'foo', 1=>'bar');
$array2 = array(0=>'bar', 1=>'foo');
$dataarrays = array($array1, $array2);
foreach($dataarrays as $dataarray) {
foreach($dataarray as $data) {
$newarray[] = $data;
}
}
print_r($newarray);
$result = array_keys(array_merge(array_flip($array1), array_flip($array2)));
var_dump($result);