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)

WBOY
Release: 2016-08-26 10:28:27
Original
1218 people have browsed it

$array = array('0'=>'3','1','2','a'=>'a','b'=>'b','c'=>'c') 
Copy after login
Copy after login

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:

第一个数组: 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)
Copy after login
Copy after login

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:

array (size=3) 'a' => string '3' (length=1) 'b' => string '1' (length=1) 'c' => string '2' (length=1) 
Copy after login
Copy after login

Reply content:

$array = array('0'=>'3','1','2','a'=>'a','b'=>'b','c'=>'c') 
Copy after login
Copy after login

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:

第一个数组: 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)
Copy after login
Copy after login

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:

array (size=3) 'a' => string '3' (length=1) 'b' => string '1' (length=1) 'c' => string '2' (length=1) 
Copy after login
Copy after login

is_numeric()

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)

$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; } }
Copy after login

$aaa = array(1=>'aa','2'=>'gg','cc'=>'dd'); foreach ($aaa as $key => $value) { if (is_numeric($key)) { echo "1".'
'; }elseif (is_string($key)) { echo "string"; } } die();
Copy after login

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

1 1 string
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!