Function returns array - PHP - php

I have this class:
class Dados{
function cadastro($store){
preg_match_all('#<div align=\'left\'>(.*?)</div>#s',$store,$dados);
return $dados[1];
}}
And this is the output of print_r($dados->cadastro($pagina_cadastro));
Array
(
[0] => 02A
[1] => M
[2] => 72 - CIÊNCIA DA COMPUTAÇÃO/N
[3] => 1402
[4] => 23/12/2013
[5] => 6/7/1995
[6] => S
[7] => M
[8] => Details Removed
[9] => Details Removed
[10] => Details Removed
[11] => Details Removed
[12] => Details Removed
[13] => Details Removed
[14] => My Email
[15] => Details Removed
[16] =>
[17] => Details Removed
[18] => Details Removed
)
Details were removed to protect my privacy
So the question is, how can use echo to print just positions 3, 7 and 9 (for example) ?
I already try $dados->cadastro[1]($pagina_cadastro); and $dados->cadastro($pagina_cadastro)[1]; but any works.
I think it's a newbie question but I really need help.
Thank you so much!

Assign the result to a variable first:
$details = $dados->cadastro($pagina_cadastro);
and then you can get any position you want by:
echo $details[3];
echo $details[7];
echo $details[9];

may be try like this:
$val = $dados->cadastro($pagina_cadastro);
echo $val[3];// similarly other also.

if this
<?php
class Dados{
function cadastro($store){
preg_match_all('#<div align=\'left\'>(.*?)</div>#s',$store,$dados);
return $dados[1];
}
}
$var = new Dados();
print_r(var->cadastro());
?>
returns your array.
Then you do like this
<?php
//Previous code before this
$array = var->cadastro();
echo $array[7] . $array[9]; //etc
?>
To specify specific values

Just assign your function call to a variable like this:
$var = $dados->cadastro($pagina_cadastro);
Then you can print any index of the array like this :
print $var[3];
print $var[7];
print $var[9];

Related

Get value of Last slash and before . in php [duplicate]

This question already has answers here:
How can I get the name of the image from url?
(8 answers)
Closed 5 years ago.
I have array like below
Array
(
[0] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg
[1] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg
[2] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR06A-961000001.jpg
[3] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR06A-96529.jpg
[4] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR07A-971000002.jpg
[5] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR07A-97530.jpg
[6] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR08A-981000003.jpg
[7] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR08A-98531.jpg
[8] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-991000004.jpg
[9] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99532.jpg
[10] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99533.jpg
[11] => C:\wamp\www\sthub\application\controllers/../../download/qr/SyR09A-99534.jpg
[12] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno53.jpg
[13] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno54.jpg
[14] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno55.jpg
[15] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno56.jpg
[16] => C:\wamp\www\sthub\application\controllers/../../download/qr/Syno57.jpg
)
I want to get value after last slash and before .jpg like SyR04A-94527,SyR05A-95528 etc..
You're trying to parse filenames, and PHP already has plenty of built-in functions to do that. There's no need to manipulate the string itself with explode or regular expressions, etc:
$filename = 'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg';
echo pathinfo($filename, PATHINFO_FILENAME);
// SyR04A-94527
Using Regx:
<?php
$a = [
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg',
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg'
];
foreach( $a AS $path ){
if( preg_match('/\/([^\/]+)\.[a-z]+$/i', $path, $match))
print_r($match[1]."\n");
}
Outputs:
SyR04A-94527
SyR05A-95528
You can test it here http://sandbox.onlinephpfunctions.com/code/081543329dda8b9e0ef59836995184171ff4ce66
I was going to use pathinfo but my sandbox site has it disabled, and I'm to lazy to turn my server on. But it would be something like this:
$a = [
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR04A-94527.jpg',
'C:\wamp\www\sthub\application\controllers/../../download/qr/SyR05A-95528.jpg'
];
foreach( $a AS $path )
echo pathinfo ($path, PATHINFO_FILENAME)."\n";
Cheers!

How can I unserialize a value from MySQL and assign the values in a variable using PHP/CodeIgniter?

Hello guys I just need a little help here about unserializing MySQL values. What I want is after getting the serialized value from my database I will assign the unserialized value in a variables.
Here's my code
//THIS IS THE QUERY
$sqlGetSerializedValues = "SELECT cscart_order_data.order_id AS order_id, cscart_order_data.data as data_serialize FROM cscart_orders
LEFT JOIN cscart_order_data
ON cscart_orders.order_id = cscart_order_data.order_id
WHERE type = 'I'";
$resultGetSerialize = $this->db->query($sqlGetSerializedValues);
$var_data = array();
foreach($resultGetSerialize->result_array() as $row1){
$var_data[] = $row1['data_serialize'];
}
unserialize($var_data); //How can i get the selected value
print_r($var_data);
OUTPUT:
Array
(
[0] => a:2:{s:6:"points";i:100;s:4:"cost";i:100;}
[1] => a:2:{s:6:"points";i:100;s:4:"cost";i:100;}
[2] => a:2:{s:6:"points";i:294;s:4:"cost";i:294;}
[3] => a:2:{s:6:"points";d:107;s:4:"cost";d:107;}
[4] => a:2:{s:6:"points";i:163;s:4:"cost";i:163;}
[5] => a:2:{s:6:"points";i:322;s:4:"cost";i:322;}
[6] => a:2:{s:6:"points";i:289;s:4:"cost";i:289;}
[7] => a:2:{s:6:"points";i:9;s:4:"cost";i:9;}
[8] => a:2:{s:6:"points";i:500;s:4:"cost";i:500;}
[9] => a:2:{s:6:"points";i:500;s:4:"cost";i:500;}
[10] => a:2:{s:6:"points";i:301;s:4:"cost";i:301;}
[11] => a:2:{s:6:"points";i:500;s:4:"cost";i:500;}
[12] => a:2:{s:6:"points";i:490;s:4:"cost";i:490;}
[13] => a:2:{s:6:"points";i:103;s:4:"cost";i:103;}
For example I want to get the "points" and assign in a variable $points.
How can i do that? An also the cost. Ok that's all Thanks in advance.
In your loop you want to call that unserialize function in there:
foreach($resultGetSerialize->result_array() as $row1){
$var_data[] = unserialize($row1['data_serialize']);
}
Then you'll wind up with a multidimensional array. Right now you're winding up with an array of serialized values.
As far as I know, the only way to extract these values (which were serialize()d by PHP, not MYSQL) is to use PHP's unserialize() function (docs here)

How to remove extra array values

I have a php script does this:
Put UPC into form
Submit form
Check to see if the UPC already exists in my database
If yes, fetch it from MySQL database
If not, fetch it from an external server, add it to MySQL database for future use
Display results
It works fine, but I'd like the variable to look exactly the same wether it comes from the MySQL cache, or the external server.
When I print it from the server, it looks like this:
Output:
[upc] => 066721020215
[pendingUpdates] => 0
[status] => success
[ean] => 0066721020215
[issuerCountryCode] => us
[found] => 1
[description] => Christie - Original Ritz Crackers
[message] => Database entry found
[size] => 225 g
[issuerCountry] => United States
[noCacheAfterUTC] => 2012-08-06T12:23:58
[lastModifiedUTC] => 2009-04-06T01:51:08
However, When I print the array from MySQL, it looks like this:
$result = mysql_query("SELECT * FROM UPC WHERE `upc` = '$codes[0]'");
$data = mysql_fetch_array($result);
echo "<pre>" . print_r($data, true) . "</pre>";
Output:
[0] => 066721020215
[upc] => 066721020215
[1] => 0
[pendingUpdates] => 0
[2] => success
[status] => success
[3] => 0066721020215
[ean] => 0066721020215
[4] => us
[issuerCountryCode] => us
[5] => 1
[found] => 1
[6] => Christie - Original Ritz Crackers
[description] => Christie - Original Ritz Crackers
[7] => Database entry found
[message] => Database entry found
[8] => 225 g
[size] => 225 g
[9] => United States
[issuerCountry] => United States
[10] => 2012-08-06
[noCacheAfterUTC] => 2012-08-06
[11] => 2009-04-06
[lastModifiedUTC] => 2009-04-06
I realize it's more or less a cosmetic difference (and an array twice as big as it needs to be), but how would I go about easily removing the 0,1,2,3,etc identifiers without looping through the array creating and manually creating a new one? Is there a function that would remove the numbered identifiers?
You need to use mysql_fetch_array($result, MYSQL_ASSOC) or mysql_fetch_assoc($result).
See http://php.net/manual/en/function.mysql-fetch-array.php and http://www.php.net/manual/en/function.mysql-fetch-assoc.php for more details.
Note that both are discouraged, as they have been replaced by the new(ish) mysqli functions: http://uk.php.net/manual/en/book.mysqli.php
Change the line:
$data = mysql_fetch_array($result);
to:
$data = mysql_fetch_assoc($result);
mysql_fetch_assoc returns the data from the db as an associative array - mysql_fetch_array returns a mixed array, with both numeric and associative indexes.
See http://php.net/manual/en/function.mysql-fetch-assoc.php

PHP Function returning mysql_fetch_assoc() results in index based array?

this is my first post here, in the past I found all my questions already solved.
Not this time so please help me :-)!
Why is $returned_assoc_arr holding an index based array instead of the associative?
I also tried it with a reference public function &fetch_assoc($res) but without luck?
Is there any way to fix this? I really need this to be an associative array..
I hope I explained everything quite well and some Senior PHP Coders can help with this.
Here we go with the code snippets:
file1.php
public function fetch_assoc($res) {
$assoc_arr = mysql_fetch_assoc($res);
echo "<pre>";
print_r($assoc_arr);
echo "</pre>";
return $assoc_arr;
}
file2.php
$returned_assoc_arr = $foo->fetch_assoc($res);
echo "<pre>";
print_r($returned_assoc_arr);
echo "</pre>";
Output from file2.php:
Array
(
[id] => 42
[Client] => 1
[DebtorAccountNumber] => 1234512345
[OrderDate] => 2001-04-03 02:00:00
[Status] => 1
[Comment] => this is a comment
[CreatedBy] => 1
[Reference] => 2083137729
)
Array
(
[0] => 42
[1] => 1
[2] => 1234512345
[3] => 2001-04-03 02:00:00
[4] => 1
[5] => this is a comment
[6] => 1
[7] => 2083137729
)
The method fetch_assoc is a member function of a class. (it's invoked on the containing object, which must be instantiated first)
what you are using in file2 is a ordinary function fetch_assoc, which definitely is different than the function described in file1.

php , SimpleXML, extract info from array

I'm trying to extract some information from ebay api . I have this link up http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=XML&appid=Morcovar-c74b-47c0-954f-463afb69a4b3&siteid=0&version=525&IncludeSelector=ItemSpecifics&ItemID=220617293997,250645537939,230485306218 and I'm using SimpleXML . So far I managed to extract all the info that I needed except the "PictureURL". The issue is that when I make a print to the simpleXMl it appears that the pictures are in an array
[GalleryURL] => http://thumbs4.ebaystatic.com/pict/1105372138158080_1.jpg
[PictureURL] => Array
(
[0] => http://i.ebayimg.com/09/!Bt+mKZQCWk~$(KGrHqYH-CYEvrwcUC47BL-,(K(pnw~~_1.JPG?set_id=8800005007
[1] => http://i.ebayimg.com/01/!Bt+mDTwB2k~$(KGrHqUH-DEEvirheBYUBL-,WtytJQ~~_1.JPG?set_id=8800005007
[2] => http://i.ebayimg.com/22/!Bt+mE8!EGk~$(KGrHqUH-CMEvsjKcE3JBL-,Wzr+sw~~_1.JPG?set_id=8800005007
[3] => http://i.ebayimg.com/17/!Bt+mFg!EGk~$(KGrHqIH-DoEvp43,)33BL-,W14vYQ~~_1.JPG?set_id=8800005007
[4] => http://i.ebayimg.com/01/!Bt+mQ0!!2k~$(KGrHqIH-EQEvqDDLQZVBL-,(j1YGg~~_1.JPG?set_id=8800005007
[5] => http://i.ebayimg.com/01/!Bt+mSq!EGk~$(KGrHqQH-C4Evs(Rz(JWBL-,(rdtsw~~_1.JPG?set_id=8800005007
[6] => http://i.ebayimg.com/03/!Bt+mUBw!Wk~$(KGrHqEH-DEEvnQtM9VkBL-,(w1+lQ~~_1.JPG?set_id=8800005007
[7] => http://i.ebayimg.com/15/!Bt+mHKQEGk~$(KGrHqQH-E!Evlr98iwBBL-,W87Nug~~_1.JPG?set_id=8800005007
[8] => http://i.ebayimg.com/13/!Bt+mI3!Bmk~$(KGrHqMH-DkEvq1,F2ooBL-,(EQ7Vg~~_1.JPG?set_id=8800005007
[9] => http://i.ebayimg.com/05/!Bt+mL2gEWk~$(KGrHqIH-EYEvov7vQY4BL-,(PzCKQ~~_1.JPG?set_id=8800005007
[10] => http://i.ebayimg.com/24/!Bt+mNlwEWk~$(KGrHqIH-CYEvqPjh6RQBL-,(Wh+S!~~_1.JPG?set_id=8800005007
[11] => http://i.ebayimg.com/19/!Bt+mPE!!2k~$(KGrHqQH-CYEvr5z9)NVBL-,(c3bzw~~_1.JPG?set_id=8800005007
)
But if I try to get them from the array I get no result . Here is the code that I currently have
$items = "220617293997,250645537939,230485306218,110537213815,180519294810";
$number_of_items = count(explode(",", $items));
$xml = $baseClass->getContent("http://open.api.ebay.com/shopping?callname=GetMultipleItems&responseencoding=XML&appid=Morcovar-c74b-47c0-954f-463afb69a4b3&siteid=0&version=525&IncludeSelector=ItemSpecifics,description&ItemID=$items");
writeDoc($xml, "api.xml");
$getvalues = simplexml_load_file('api.xml');
$picture_url = $getvalue->Item[$number]->PictureURL[2];
echo "picture url is $picture_url";
Using the above code I was expecting to extract the 2nd picture url from the array .
thank you in advance for any help !
Can't test this, I'm afraid, but have you tried var_dump($picture_url) to see what $picture_url is? Maybe it needs to be cast to string?
echo "picture url is" . (string)$picture_url;

Categories