Parse query string into two dimensional array - php

I am working with my code, that can convert a query string into array.
This is my query string:
key1=['PH','PHILIPPINES']&key2=['KR','KOREA']
And I want only to display the second row, something like this:
Philippines
Korea
In my code, it's working but it displays:
['PH','PHILIPPINES']
['KR','KOREA
Here is my code:
<?php
$get_string = "key1=['PH','PHILIPPINES']&key2=['KR','KOREA']";
parse_str($get_string, $get_array);
$colors = $get_array;
foreach ($colors as $key => $value) {
echo $value."<BR>";
}
?>

This is your code:
<?php
$get_string = "key1=['PH','PHILIPPINES']&key2=['KR','KOREA']";
parse_str($get_string, $get_array);
$colors = $get_array;
foreach ($colors as $key => $value) {
// remove first and last characters ( "[" and "]" )
$value = substr($value, 1, -1);
// explode
$value = explode(",", $value);
// remove "'"
$our_value = str_replace("'", "", trim($value[1]));
// show
echo ucfirst(strtolower($our_value))."<BR>";
}
?>

I think you need this.
just simple explode by comma , and preg_replace to get only alphabates.
$get_string = "key1=['PH','PHILIPPINES']&key2=['KR','KOREA']";
parse_str($get_string, $get_array);
$colors = $get_array;
foreach ($colors as $key => $value) {
$x = explode(",",$value);
$new_string = preg_replace("/[^A-Za-z0-9?!]/",'',$x[1]);
echo $new_string."<br>";
}
Output :
PHILIPPINES
KOREA

$var = array(
'PH'=>'PHILLIPINES',
'KR'=>'KOREA');
Else
$var= array(
array('KR'=>'KOREA'),
array('PH'=>'PHILLIPINES')
);
Then echo in
foreach loop.

Related

create php variable from string

Hello i have a script that extract company names from a string. I want that the extracted names to be converted to php variable. So for example first result Real Coffee Sweeden must be converted to $RealCoffeeSweeden = 0 so i can assign a value to it
$test='/showname/0406741848 : Real Coffee Sweeden
/showname/0406741849 : Healthzone SE
/showname/16133663413 : FREE
/showname/16133663414 : RadioPlantes Canada
/showname/16133663417 : Dealsoftoday.Eu Canada
/showname/16136995593 : FREE
/showname/16136995594 : Club White Smile Canada
/showname/16138007442 : FREE
/showname/16138007547 : Mybitwave.com Canada
/showname/16465596150 : BABY
/showname/16465696956 : FREE
/showname/16465696957 : FREE
/showname/16467419944 : Mybitwave.com UK
/showname/16469181975 : FREE
/showname/21501350 : SecureSurf.EU NO
/showname/21501351 : FileCloud365 Norwegian
/showname/21501352 : FREE
/showname/21501353 : RadioPlantes Norwegian
';
$myRows= explode("\n", $test);
foreach( $myRows as $key => $value) {
$pieces = explode(":", $value);
$result[] = $pieces[1];
}
foreach ($result as $res){
$res // covert to php variable
//example: $RealCoffeeSweeden = 0;
}
You can try it this way
$my_array = explode("\n", $test);
foreach($my_array as $key => $value {
$my_string = explode(':', $value)
${str_replace(' ','', $my_string[1])} = $my_string;
echo $$my_string;
}
You should use an array for that. But if you want to do it the way you write, you can simply do something like that:
foreach( $myRows as $key => $value) {
$pieces = explode(":", $value);
$res = str_replace(' ', '', $pieces[1]); // replace whitespaces for valid names
$$res = 0; // note the double dollar signs
}
If you want to use an array tho, do something like this:
$result = [];
foreach( $myRows as $key => $value) {
$pieces = explode(":", $value);
$key = str_replace(' ', '', $pieces[1]);
$result[$key] = 0;
}
According to your comment, change second last line in the foreach loop with following:
$res = str_replace(' ', '', $res) . '_f';

exploade accosiative array type data

I have data like following
string(133) "lindsey#testmail.com=>5.jpg,rickey#testmail.com=>6.jpg,darnell#testmail.com=>84.jpg,ball#gmail.com =>49.jpg,norton#tesing.com=>68.jpg"
i want to explode email and image separately.
i use explode but it didn't work.
i also try associative array.
here is my code but it didn't work.
foreach ($array as $key => $value ) {
echo $key;
echo "<li><img src=\"".base_url()."images/menters/".$values."\" class=\"img-border\"/><span>icon</span></li>\n";
}
i think it happen additional string(133)
i have no idea how to accomplish this
You can't do that with a single explode, you have to explode the string twice. You can then use an associative array to store values and use them
$array = explode(',' $string);
foreach ($array as $key => $val) {
$exp = explode('=>', $val);
$assoc_array[$key]['mail'] = $exp[0];
$assoc_array[$key]['img'] = $exp[1];
}
foreach ($assoc_array as $val) {
echo 'Mail : ', $val['mail'], '<br>';
echo 'Image : ', $val['img'];
}

PHP Print R Foreach Array, getting a string to post as array

Driving me crazy but here goes:
$cat=2,3,4
$test = print_r(explode(',', $cat), true);
echo ''.$test.'';
foreach ($test as $key => $val) {
echo "$key => $val <br>";
}
What I'm hoping to get:
2
3
4
I'm trying to use a string of numbers obtained from another code to build an array, and then show each value on separate lines. What am I doing wrong?
You shouldn't assign print_r() to $test variable... It's a debugging function for printing purposes..
You should write your code like this..
$cat='2,3,4';
foreach(explode(',',$cat) as $v)
{
echo $v."<br>";
}
This should be either:
$test = explode(',', $cat);
or:
print_r(explode(',', $cat), true);
<?php
$cat='2,3,4';
$array = explode(',', $cat);
$test = print_r($array, true);
echo ''.$test.'';
foreach ($array as $key => $val) {
echo "$key => $val <br>";
}
$test is string. You need array for foreach statement.
Try with this:
<?php
$cat = "2,3,4"; // This is a string separated with commas.
$test = explode(',', $cat); // This assign every number to an array item. The print_r() function is not necesary here.
foreach ($test as $value) {
echo $value. "<br />\n";
}
?>
THIS WILL PRODUCE:
2
3
4
Explanation about your errors:
$cat= '2,3,4'; <- ** Missing quotes **
$test = print_r(explode(',', $cat), true); <- **print_r is not needed here **
echo ''.$test.'';
foreach ($test as $key => $val) { <- Not using it propperly
echo "$key => $val ";
}
You miss ; in $cat variable declaration. Use the following code.
$cat=2,3,4;
$test = explode(',', $cat);
foreach ($test as $key => $val) {
echo $val."<br>";
}

How to echo out the values of this array?

How to echo out the values individually of this array?
Array ( [0] => 20120514 [1] => My Event 3 )
so
echo $value[0]; etc
I have this so far:
foreach (json_decode($json_data_string, true) as $item) {
$eventDate = trim($item['date']);
// positive limit
$myarray = (explode(',', $eventDate, 2));
foreach ($myarray as $value) {
echo $value;
}
This echo's out the whole string no as an array. and if i do this?
echo $value[0};
Then I only get 2 characters of it??
The print_r :
Array ( [0] => 20120430 [1] => My Event 1 )
foreach ($array as $key => $val) {
echo $val;
}
Here is a simple routine for an array of primitive elements:
for ($i = 0; $i < count($mySimpleArray); $i++)
{
echo $mySimpleArray[$i] . "\n";
}
you need the set key and value in foreach loop for that:
foreach($item AS $key -> $value) {
echo $value;
}
this should do the trick :)
The problem here is in your explode statement
//$item['date'] presumably = 20120514. Do a print of this
$eventDate = trim($item['date']);
//This explodes on , but there is no , in $eventDate
//You also have a limit of 2 set in the below explode statement
$myarray = (explode(',', $eventDate, 2));
//$myarray is currently = to '20'
foreach ($myarray as $value) {
//Now you are iterating through a string
echo $value;
}
Try changing your initial $item['date'] to be 2012,04,30 if that's what you're trying to do. Otherwise I'm not entirely sure what you're trying to print.
var_dump($value)
it solved my problem, hope yours too.

Basic implode foreach

I have the following code of which I want to echo array elements separated by commas. The code outputs the disered list, but without commas. What am I missing?
<?php
$array = get_field('casts');
$elements = $array;
foreach($array as $key => $value) {
echo implode(', ', $value)};
?>
EDIT 1: where $elements are nested arrays.
EDIT 2: Working snippet:
<?php
$array = get_field('casts');
$new_array = array();
foreach($array as $sub_array) {
foreach($sub_array as $value) {
array_push($new_array, $value);
}
}
echo implode(", ", $new_array);
?>
Why are you assigning $elements = $array; and then never using $elements?
Also you don't need to loop (foreach) to implode an array.
Try this:
<?php
$array = get_field('casts');
$new_array = array();
foreach($array as $sub_array) {
foreach($sub_array as $value) {
// this array_push() function adds $value to the end of $new_array.
array_push($new_array, $value);
}
}
echo implode(", ", $new_array);
?>
Here is the documentation on implode()
You can play around and test the above code here.
Also next time, add the tag php, otherwise our codes won't get color syntax.

Categories