How can I place last index as first index in an array. Suppose I have an array which looks like this.
Array
(
[0] => ABC
[1] => abc
[2] => rodriguez
[3] => Barkleys, 15 NO.
[4] => A
[5] => 1234567890
[6] =>
[7] => YES
[8] =>
[9] => 1
)
Now I want to show this array like this.
Array
(
[0] => 1
[1] => ABC
[2] => abc
[3] => rodriguez
[4] => Barkleys, 15 NO.
[5] => A
[6] => 1234567890
[7] =>
[8] => YES
[9] =>
)
How can I get this Please suggest.
Try this as an alternative way,
array_unshift($arr, $arr[count($arr)-1]);
unset($arr[count($arr)-1]);
$arr = array_values($arr);
print_r($arr);
Give it a try, this should work.
You can also do this
$ar = array('a', 'b', 'c');
//pop out the last index from the array
$shift = (array_pop($ar));
//prepend it to the beginning
array_unshift($ar, $shift);
print_r($ar);
Output:
Array (
[0] => c
[1] => a
[2] => b
)
Related
here's the result of my array
Array
(
[1] => Array
(
[0] => ABC
[1] => abc
[2] => rodriguez
[3] => Barkleys, 15 NO.
[4] => A
[5] => 1234567890
[6] =>
[7] => YES
[8] => a
)
[2] => Array
(
[0] => DEF
[1] => def
[2] => DAM
[3] => Barkleys, 15 NO.
[4] => A
[5] => 1234567891
[6] =>
[7] => YES
[8] => b
)
[3] => Array
(
[0] => GHI
[1] => ghi
[2] => TEG
[3] => Street4
[4] => B
[5] => 1234567892
[6] => YES
[7] =>
[8] => c
)
[4] => Array
(
[0] => JKL
[1] => jkl
[2] => CHA
[3] => Street4
[4] => B
[5] => 1234567893
[6] => YES
[7] =>
[8] => d
)
)
I need some of values from this array by using other value for exp. if I have value of 4th index in two separate arrays.
array (
[0] => A
[1] => A
)
array (
[0] => B
[1] => B
)
Now I need to get values of 2nd index in two seperate array using above two array value
array (
[1] => rodriguez
[2] => DAM
)
array (
[1] => TEG
[2] => CHA
)
How can I do this please suggest solution.
You can try to do this way :
<?php
$firstArray = array();
foreach($bigArray as $key => $array) {
$value = $array[4]; //fetch the 4th indice
if(! isset($firstArray[$value])) {
$firstArray[$value] = array();
}
$firstArray[$value][$key] = $value;
}
$secondArray = array();
foreach($firstArray as $name => $array) {
foreach($array as $key => $info) {
$value = $bigArray[$key][2]; //fetch the 2nd indice
if(! isset($secondArray[$value])) {
$secondArray[$value] = array();
}
$secondArray[$value][$key] = $value;
}
}
my code is:
array set 1:
Array
(
[0] => 15-3
[1] => 16-3
[2] => 15-4
[3] => 16-4
[4] => 15-3
[5] => 16-3
[6] => 15-4
[7] => 16-4
[8] => 15-3
[9] => 16-3
[10] => 15-4
[11] => 16-4
)
My second array set is:
Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 2
[5] => 2
[6] => 2
[7] => 2
[8] => 3
[9] => 3
[10] => 3
[11] => 3
)
i just combine the above both the two array into one like below
$data1=array_combine($store_attri_ids, $store_ids);
but its shows like that
Array
(
[15-3] => 3
[16-3] => 3
[15-4] => 3
[16-4] => 3
)
The remaining values are not combined, wt we do nw????
You can try this by creating a sub-array -
$data1 = array();
foreach($store_attri_ids as $key => $id) {
$data1[$id][] = $store_ids[$key];
}
The output would be like -
Array
(
[15-3] => array(1, 2, 3),
[16-3] => array(...),
[15-4] => array(...),
[16-4] => array(...)
)
If you use array_combine the result is totally correct, like putting here: http://php.net/manual/en/function.array-combine.php
You need use array_merge, take a look at the documentation :http://php.net/manual/en/function.array-merge.php
Use like this:
$data1=array_merge($store_attri_ids, $store_ids);
print_r($data1);
How to explode this array value:
$row = array(
[0] = 1,1,Carlyle,Rogers,1,"Carlyle, Rogers",0000-00-00,,carlyle.rogers#stafford-trust.com,,non premium)
I tried this code
$values = explode(',', $row[0]);
and give me this wrong output:
Array (
[0] => 1
[1] => 1
[2] => Carlyle
[3] => Rogers
[4] => 1
[5] => "Carlyle
[6] => Rogers"
[7] => 0000-00-00
[8] =>
[9] => carlyle.rogers#stafford-trust.com
[10] =>
[11] => non premium
)
What I want is to Output must be like this one:
Array (
[0] => 1
[1] => 1
[2] => Carlyle
[3] => Rogers
[4] => 1
[5] => "Carlyle, Rogers"
[6] => 0000-00-00
[7] =>
[8] => carlyle.rogers#stafford-trust.com
[9] =>
[10] => non premium
)
You can't use explode like that because your input seems to be CSV-formatted and explode knows nothing about that "format". Use str_getcsv instead:
$values = str_getcsv($row[0]);
I have an array encoded in UTF-8, sorta like this:
Array
(
[0] => אלף
[1] => בית
[2] => גימל
[3] => דלת
[4] => הא
[5] => ואו
)
Is it possible to leave the 0 array item empty and start the array at 1? I sorta wanna nudge everything over, and the array would then look like this:
Array
(
[0] =>
[1] => אלף
[2] => בית
[3] => גימל
[4] => דלת
[5] => הא
[6] => ואו
)
Thanks!
Use array_unshift, example:
<?php
$arr=array(1,2,3);
array_unshift($arr,null);
print_r($arr);
?>
Prints Array ( [0] => [1] => 1 [2] => 2 [3] => 3 )
i have the following array and want to get rid/remove the empty array and rearrange it in an order.can anyone help me please.
Array
(
[ufile] => Array
(
[name] => Array
(
[0] => chicken soup.jpg
[1] =>
[2] => hot n sour sup.jpg
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[type] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[tmp_name] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
[7] =>
[8] =>
)
[error] => Array
(
[0] => 1
[1] => 4
[2] => 1
[3] => 4
[4] => 4
[5] => 4
[6] => 4
[7] => 4
[8] => 4
)
[size] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
)
)
)
I believe this will cleanup your array: remove empty elements (those that evaluate to empty, eg "" and 0 equally), remove duplicate elements, and sort it.
$cleaned = array_map('array_filter', $array_to_be_cleaned);
$cleaned = array_map('array_unique', $cleaned);
$cleaned = array_map('sort', $cleaned);
To filter out the empty elements of the array, check out array_filter.
To sort the elements, check out sort (or refer to this list of sorting functions to see what meets your needs)
$newarray = array_filter($myarray);
sort($newarray);
This will take the array you pass it (ie. $myarray) and it will strip out any empty values, then it will store the results to $newarray. After that, sort will organize the remaining values from least to greatest.
unset( $var['ufile']['type'] );
unset( $var['ufile']['tmp_name'] );
To sort, use sort(), usort(), or any other you want. Here's a good comparison of the different sort methods.
Try array_filter($array). It will remove all NULL elements and return the cleared array.