Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 18th, 2003, 02:22 PM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating an associatve array from a string

Hi, I have a client server application where the client receives the $data string in example (2). I want the data to be assigned or converted to an associative array. Is there a way to do this?

Example (1) This is in theory how it should work.
$jess = array ("Alt_id" => "10040" ,"Profile" => "Married");
extract ($jess);
print ("Alt_id = $Alt_id\n");
print ("Profile = $Profile\n");
# The values get printed properly

Example (2) This is how I would like it to work, but it does not.
$data = ' "Alt_id" => "10040" ,"Profile" => "Married" ';
$jess = array ($data);
extract ($jess);
print ("Alt_id = $Alt_id\n");
print ("Profile = $Profile\n");
# Unfortunately there are no values printed.

Walter G

 
Old August 18th, 2003, 02:37 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

For something like that you should use eval().

$jess = eval("array({$data})");

 http://www.php.net/eval

The problem with your example 2 is that you were only passing a single string to array(), so your array was created with a single numerical index (0) with a string value of ' "Alt_id" => "10040" ,"Profile" => "Married" '.


Take care,

Nik
http://www.bigaction.org/
 
Old August 19th, 2003, 01:43 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oops! I'm assinging the return value of eval() to $jess. According to the documentation:

Quote:
quote:from http://www.php.net/eval
A return statement will terminate the evaluation of the string immediately. In PHP 4, eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned. In PHP 3, eval() does not return a value.

To do it properly, you need one of two options:

Option 1: Assign $jess inside eval()
  eval("\$jess = array({$data})");

Option 2: Use the 'return' keyword to return the new array from eval().
  $jess = eval("return array({$data})");

I like the second method better, personally.


Take care,

Nik
http://www.bigaction.org/
 
Old August 19th, 2003, 02:53 PM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Nik I tried it both ways you suggested and I still get a parsing
error.
I have also tried an alternate version of my $data being:
$data = '("Alt_id" => "10040" ,"Profile" => "Married");';


<?php
  $data = '"Alt_id" => "10040" ,"Profile" => "Married"';
  eval("\$jess = array({$data})");
  extract ($jess);
  print ("Alt_id = $Alt_id\n");
  print ("Profile = $Profile\n");
?>


<?php
  $data = '"Alt_id" => "10040" ,"Profile" => "Married"';
  $jess = eval("return array({$data})");
  extract ($jess);
  print ("Alt_id = $Alt_id\n");
  print ("Profile = $Profile\n");
?>


I am running PHP 4.1.2.

Do these examples print the correct values for you?
The output should be:
Alt_id = 10040
Profile = Married

Thanks again,


Walter G.

 
Old August 19th, 2003, 02:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Walter,

The parse error is due to a typo on my part -- a missing semicolon to terminate the statement to be evaluated. That's why you get an "unexpected end in eval()'d code" error.

$data = '"Alt_id" => "10040" ,"Profile" => "Married"';
$jess = eval("return array({$data});");

extract ($jess);

print ("Alt_id = $Alt_id\n");
print ("Profile = $Profile\n");


That should work now!


Take care,

Nik
http://www.bigaction.org/
 
Old August 19th, 2003, 02:59 PM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks this is just what I was looking for.

Walter G.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Convering a String Array to an Integer array nkrust C# 9 November 17th, 2010 12:02 PM
Casting String array to string Samatha ASP.NET 1.0 and 1.1 Professional 1 December 5th, 2006 07:46 AM
creating array hiruni Beginning VB 6 5 February 9th, 2006 03:22 AM
Dynamically creating an array youngj PHP How-To 0 November 8th, 2004 03:42 AM
trouble creating array Tachyon Beginning PHP 3 June 28th, 2004 11:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.