Home >Backend Development >PHP Tutorial >How to use PHP to split an array into two new arrays based on conditions, and save the new array as an English key? (Already given as an example in the title)
<code>$array = array('0'=>'3','1','2','a'=>'a','b'=>'b','c'=>'c') </code>
Question 1:
Assign the previous indexes 0, 1, 2 to the $int array, and allocate the English keys such as a, b, c to the $string array.
Distinguish between 0, 1, 2 and English keys I don’t know how to deal with the problem because they are all string types? (solved)
Question 2:
<code>第一个数组: array (size=3) 0 => string '3' (length=1) 1 => string '1' (length=1) 2 => string '2' (length=1) 第二个数组: array (size=3) 0 => array (size=1) 'a' => string 'a' (length=1) 1 => array (size=1) 'b' => string 'b' (length=1) 2 => array (size=1) 'c' => string 'c' (length=1)</code>
After var_dump, I used array_push for the second array, but I hope the index is not in numbers, but directly in English,
as follows:
<code>array (size=3) 'a' => string '3' (length=1) 'b' => string '1' (length=1) 'c' => string '2' (length=1) </code>
<code>$array = array('0'=>'3','1','2','a'=>'a','b'=>'b','c'=>'c') </code>
Question 1:
Assign the previous indexes 0, 1, 2 to the $int array, and allocate the English keys such as a, b, c to the $string array.
Distinguish between 0, 1, 2 and English keys I don’t know how to deal with the problem because they are all string types? (solved)
Question 2:
<code>第一个数组: array (size=3) 0 => string '3' (length=1) 1 => string '1' (length=1) 2 => string '2' (length=1) 第二个数组: array (size=3) 0 => array (size=1) 'a' => string 'a' (length=1) 1 => array (size=1) 'b' => string 'b' (length=1) 2 => array (size=1) 'c' => string 'c' (length=1)</code>
After var_dump, I used array_push for the second array, but I hope the index is not in numbers, but directly in English,
as follows:
<code>array (size=3) 'a' => string '3' (length=1) 'b' => string '1' (length=1) 'c' => string '2' (length=1) </code>
is_numeric()
<code class="php">$arr1 = $arr2 = []; $arr = array('0'=>'3','1','2','a'=>'a','b'=>'b','c'=>'c'); foreach ($arr as $key=>$value) { if (is_numeric($key)) { $arr1[$key] = $value; } else { $arr2[$key] = $value; } }</code>
<code>$aaa = array(1=>'aa','2'=>'gg','cc'=>'dd'); foreach ($aaa as $key => $value) { if (is_numeric($key)) { echo "1".'</br>'; }elseif (is_string($key)) { echo "string"; } } die();</code>
Execute the code breaking test in the above paragraph. Whether it is '1' or 1, it is a number. 'ccsdad' and this kind are str. So you can judge how to combine two new arrays and it will be ok
This is the result
<code>1 1 string</code>