THis this my code .
$data = array(
'24 Jan|8:30' => '12.6',
'22 Feb|8:30' => '250',
'11 Mar|8:10' => '0',
'31 Apr|23:30' => '7',
'32 Apr|23:30' => '80',
'33 Apr|23:30' => '67',
'34 r|23:30' => '45',
'35 Ap|23:30' => '66',
'34 Lr|23:30' => '23',
'3 Apr|23:30' => '23'
);
//echo serialize($data);
$x = unserialize('a:10:{s:12:"24 Jan|8:30 ";s:4:"12.6";s:12:"22 Feb|8:30 ";s:3:"250";s:12:"11 Mar|8:10 ";s:1:"0";s:12:"31 Apr|23:30";s:1:"7";s:12:"32 Apr|23:30";s:2:"80";s:12:"33 Apr|23:30";s:2:"67";s:12:"34 r|23:30 ";s:2:"45";s:12:"35 Ap|23:30 ";s:2:"66";s:12:"34 Lr|23:30 ";s:2:"23";s:12:"3 Apr|23:30 ";s:2:"23";}');
var_dump($x);
Not work in unserialize function.
Please help!
The serialized representation of $data and the string you are trying to unserialize differ.
http://codepad.viper-7.com/3zlk1a
At offset 199 you see
s:12:"34 r|23:30 "
but the string (s) isn't 12 characters long (thats what s:12: mean). I guess something modified the serialized string directly. Just don't do it :) Always unserialize and work with the structured values.
'a:10:{s:12:"24 Jan|8:30 ";s:4:"12.6";s:12:"22 Feb|8:30 ";s:3:"250";s:12:"11 Mar|8:10 ";s:1:"0";s:12:"31 Apr|23:30";s:1:"7";s:12:"32 Apr|23:30";s:2:"80";s:12:"33 Apr|23:30";s:2:"67";s:12:"34 r|23:30 ";s:2:"45";s:12:"35 Ap|23:30 ";s:2:"66";s:12:"34 Lr|23:30 ";s:2:"23";s:12:"3 Apr|23:30 ";s:2:"23";}'
...is not a valid serialization. Specifically, the s:12:"34 r|23:30 "; segment indicates that the string 34 r|23:30 contains 12 characters, which it does not.
$a = serialize($data);
$x = unserialize($a);
Related
I have a multidimensional array as follows, which is a PHP array of shoe sizes and their conversions...
$size_array = array(
"M"=>array(
'6'=> array('uk'=>'6','eu'=>'39.5','us'=>'7'),
'6H'=> array('uk'=>'6.5','eu'=>'40','us'=>'7.5'),
'7'=> array('uk'=>'7','eu'=>'40.5','us'=>'8'),
'7H'=> array('uk'=>'7.5','eu'=>'41','us'=>'8.5'),
'8'=> array('uk'=>'8','eu'=>'42','us'=>'9'),
'8H'=> array('uk'=>'8.5','eu'=>'42.5','us'=>'9.5'),
'9'=> array('uk'=>'9','eu'=>'43','us'=>'10'),
'9H'=> array('uk'=>'9.5','eu'=>'44','us'=>'10.5'),
'10'=> array('uk'=>'10','eu'=>'44.5','us'=>'11'),
'10H'=> array('uk'=>'10.5','eu'=>'45','us'=>'11.5'),
'11'=> array('uk'=>'11','eu'=>'46','us'=>'12'),
'11H'=> array('uk'=>'11.5','eu'=>'46.5','us'=>'12.5'),
'12'=> array('uk'=>'12','eu'=>'47','us'=>'13'),
'12H'=> array('uk'=>'12.5','eu'=>'48','us'=>'13.5'),
'13'=> array('uk'=>'13','eu'=>'48.5','us'=>'14')
),
"F"=>array(
'3'=> array('uk'=>'3','eu'=>'35.5','us'=>'5'),
'3H'=> array('uk'=>'3.5','eu'=>'36','us'=>'5.5'),
'4'=> array('uk'=>'4','eu'=>'37','us'=>'6'),
'4H'=> array('uk'=>'4.5','eu'=>'37.5','us'=>'6.5'),
'5'=> array('uk'=>'5','eu'=>'38','us'=>'7'),
'5H'=> array('uk'=>'5.5','eu'=>'38.5','us'=>'7.5'),
'6'=> array('uk'=>'6','eu'=>'39','us'=>'8'),
'6H'=> array('uk'=>'6.5','eu'=>'39.5','us'=>'8.5'),
'7'=> array('uk'=>'7','eu'=>'40','us'=>'9'),
'7H'=> array('uk'=>'7.5','eu'=>'41','us'=>'9.5'),
'8'=> array('uk'=>'8','eu'=>'41.5','us'=>'10'),
'8H'=> array('uk'=>'8.5','eu'=>'42.5','us'=>'10.5'),
'9'=> array('uk'=>'9','eu'=>'43','us'=>'11'),
'9H'=> array('uk'=>'9.5','eu'=>'43.5','us'=>'11.5'),
'10'=> array('uk'=>'10','eu'=>'44','us'=>'12')
)
);
The array is part of a function that returns the conversions based on a supplied size and gender (i.e. SizeConvert('M','6') returns Array ([uk] => 6, [eu] => 39.5,[us] => 7)).
I want to extend the function to allow the passing of a value which will return the array results with any .5 values replaced with ½ (or ½) (i.e. SizeConvert('M','6','Y') returns Array ([uk] => 6, [eu] => 39½,[us] => 7))
How do I make str_replace (or a more appropriate command) iterate over the array and replace the values?
I've tried something like str_replace(".5", "½", $size_array) but I guess that's not working as it's only looking at the initial array, not the sub-arrays.
You are trying to apply this to a multidimensional array without real reason. If you have your SizeConvert function ready and returning a one dimensional array, simply apply the transformation before returning the value:
function SizeConvert(/* other parameters */, bool $convertOneHalf) {
$match = ... // your code to find the match
return $convertOneHalf
? str_replace('.5', '½', $match)
: $match;
}
Based on the boolean value of the parameter that dictates whether the conversion should be applied, we either return the modified or the unmodified result through the ternary.
Do not overthink it and use a for loop to loop through all the elements in the array and use an if...else... to check for 0.5
if($array[index]=="0.5") {
$array[index]="½";
} else {
$array[index]=str_replace(".5", "½", $array[index]);
}
I coded up a simple code, it's not exactly the answer to your question but u can use the logic behind it. The code below will change all the 0.5 in the array to 1⁄2 but since u already acquire the data, there is no need to have so much nested-loop, just 1 level of the loop to loop through all ur elements in your array is enough.
<?php
$size_array = array(
"M" => array(
'6' => array(
'uk' => '6',
'eu' => '39.5',
'us' => '7'
) ,
'6H' => array(
'uk' => '6.5',
'eu' => '40',
'us' => '7.5'
) ,
'7' => array(
'uk' => '7',
'eu' => '40.5',
'us' => '8'
)
) ,
"F" => array(
'3' => array(
'uk' => '3',
'eu' => '35.5',
'us' => '5'
) ,
'3H' => array(
'uk' => '3.5',
'eu' => '36',
'us' => '5.5'
) ,
'4' => array(
'uk' => '4',
'eu' => '37',
'us' => '6'
)
)
);
foreach ($size_array as $firstLevel)
{
foreach ($firstLevel as $secondLevel)
{
foreach ($secondLevelas $values)
{
if ($values== "0.5")
{
echo $values= "½";
}
else
{
echo $values= str_replace(".5", "½", $values);
}
}
}
}
?>
I know how to check for a value in an array, but how do I check for a value in an Array Iterator?
$array = new ArrayIterator(array(
'1QmRjtsw2UQ' => array('pubdate' => '26 Jun 15', 'alt' => '8 Year Old Beautifully Covers Thinking Out Loud', 'anchor text' => '8-yo \'Thinking Out Loud\''),
'eKqLaYrcf3A' => array('pubdate' => '25 Jun 15', 'alt' => 'Plane Lands On Truck', 'anchor text' => 'Plane Lands On Truck'),
));
I'm trying to check for the values such as 1QmRjtsw2UQ.
This does not work:
if(in_array('1QmRjtsw2UQ', $array));
why don't you use array_key_exists ?
if(array_key_exists('1QmRjtsw2UQ', $array))
{
// do something
}
Try this,
$array->offsetExists('1QmRjtsw2UQ');
[-27439367, 160818667, 'http:\/\/cs13110.vk.me\/u109515688\/video\/l_97403fde.jpg', 'Super Bass', '', '0', 38674081, 37, 0, '2:34', '3', '_8cb245a336c2e35049', '']
Hello! Here is my sample text.... I need to use preg_match for multiple patterns... I need to find:
1. -27439367
2. 165375317
3. http://cs6067.vk.me/u189929178/video/l_02613a05.jpg
4. Super Bass
5. 0
6. 38674081
7. 37
8. 2:34
9. 0
10. 3
11. _8cb245a336c2e35049
I used:
preg_match_all("/[(.*?), (.*?), '(.*?)', '(.*?)', '', '0', 0, 23, 0, '', '0', '(.*?)', '']/mis", $a, $hashtweet);
Here you go:
$json = "[-27439367, 160818667, 'http:\/\/cs13110.vk.me\/u109515688\/video\/l_97403fde.jpg', 'Super Bass', '', '0', 38674081, 37, 0, '2:34', '3', '_8cb245a336c2e35049', '']" ;
$json = preg_replace("/'/", '"', $json); //Replace single quotes by double quotes
$obj = json_decode($json);
var_dump($obj);
array (size=13)
0 => int -27439367
1 => int 160818667
2 => string 'http://cs13110.vk.me/u109515688/video/l_97403fde.jpg' (length=52)
3 => string 'Super Bass' (length=10)
4 => string '' (length=0)
5 => string '0' (length=1)
6 => int 38674081
7 => int 37
8 => int 0
9 => string '2:34' (length=4)
10 => string '3' (length=1)
11 => string '_8cb245a336c2e35049' (length=19)
12 => string '' (length=0)
Maybe strip the [] brackets and do explode(',', $input) (docs)?
Another idea: this looks like a valid JSON data, so json_decode (docs) should do the trick.
Hey all i am new at classes and arrays in php but need to find out how to go about getting the correct value from this class function array
class theCart {
public static $DISTANCE = array(
'0' => '0 - 75',
'10' => '76 - 125',
'20' => '126 - 175',
'30' => '176 - 225',
'40' => '226 - 275',
'50' => '276 - 325'
);
}
My output i am trying to match looks like this: 76 - 125
Do i just call it like
$distanceNum = '76 - 125';
$tmpDistanceTotal = $DISTANCE($distanceNum);
Should $tmpDistanceTotal then have a value of 10? I'm thinking that the array only has the values 0,10,20,30,40,50 in it?
I have another array:
public static $STEPS = array(
'0' => 0,
'1' => 0,
'2' => 0,
'3' => 25,
'4' => 50,
'5' => 75,
'6' => 100,
'7' => 125
);
My output i am trying to match with that above is 3 I'm not sure if its looking for a string or not?
This should clear the point:
foreach (theCart::$DISTANCE as $k => $v) {
if ($v == '76 - 125') {
echo $k;
break;
}
}
For $tmpDistanceTotal to get the value 10, you could do the following:
$tmpDistanceTotal = array_search($distanceNum, theCart::DISTANCE);
or you may wish to end up with something like this:
class theCart {
public static $DISTANCE = array(
'0' => '0 - 75',
'10' => '76 - 125',
'20' => '126 - 175',
'30' => '176 - 225',
'40' => '226 - 275',
'50' => '276 - 325'
);
public function getTotalDistance($distanceNum)
{
return array_search($distanceNum, self::DISTANCE);
}
}
Your question is actually just about arrays, and you should remove the classes from here to make things easier to understand:
$DISTANCE = array(
'0' => '0 - 75',
'10' => '76 - 125',
'20' => '126 - 175',
'30' => '176 - 225',
'40' => '226 - 275',
'50' => '276 - 325'
);
$variable = $DISTANCE[10];
In the above example Variable will be equal to 76-125. You're working with Associative Arrays, so you need to go read up on them a little bit as your questions shows you don't really understand how arrays work. Once you have that down, go ahead and move into a class context like you mentioned above.
You can check out the PHP Manual here: http://php.net/manual/en/language.types.array.php
For a short and quick answer you can use
$tempVar = 10;
$tmpDistance = $this->DISTANCE[$tempVar];
Not sure what you are trying to do, but you could use array_search:
$distanceNum = '76 - 125';
$key = array_search($distanceNum, theCart::$DISTANCE);
$key is now 10.
How can I parse a CSV like this one in PHP (there's a double quote near value 8)?
"03720108";"value 8"";"";"219";"03720108";"value";"value";"value";"";"";"";"";"";"";"value";"";"";"value";"value";
I tried with fgetscv($pointer, 4096, ';', '"');
Your data seems to be malformed starting at the prior line. You have an opening quote with no closing quote.
Yes. Notice the extra quote.
"value 8"";
You might still be able to parse this string, though:
$str = '"03720108";"value 8"";"";"219";"03720108";"value";"value";"value";"";"";"";"";"";"";"value";"";"";"value";"value";';
$parsed = array_map(
function( $str ) { return substr($str, 1, -1); },
explode(';', $str)
);
var_export($parsed);
/*
array (
0 => '03720108',
1 => 'value 8"',
2 => '',
3 => '219',
4 => '03720108',
5 => 'value',
6 => 'value',
7 => 'value',
8 => '',
9 => '',
10 => '',
11 => '',
12 => '',
13 => '',
14 => 'value',
15 => '',
16 => '',
17 => 'value',
18 => 'value',
19 => false,
)
*/
Things get a bit more complicated, though, if there are elements that contain a ; character (normally would be escaped by enclosing the value in quotes... d'oh!), and the above code assumes that you only need to parse a single line (though if you are using fgets() to read the input stream, you should be OK).